package com.sbs.gaoxinqutoilet.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.appcompat.widget.AppCompatButton;

import com.sbs.gaoxinqutoilet.utils.ScreenUtil;


/**
 * Created by 聪明一只哈 on 2018/6/11.
 * Time: 2018/6/11  14:26
 * 悬浮按钮1
 */

public class MyDragButton extends AppCompatButton implements View.OnTouchListener{

    int lastX, lastY;
    int originX, originY;
    final int screenWidth ;
    final int screenHeight ;

    public MyDragButton(Context context) {
        this(context, null);
    }

    public MyDragButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyDragButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        screenWidth = ScreenUtil.getScreenWidth(context);
        screenHeight = ScreenUtil.getContentHeight(context);
        Log.i("zxc","w:"+screenWidth+"~~h:"+screenHeight);
        setOnTouchListener(this);
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {

        int ea = event.getAction();
        switch (ea) {
            case MotionEvent.ACTION_DOWN:
                lastX = (int) event.getRawX();// 获取触摸事件触摸位置的原始X坐标
                lastY = (int) event.getRawY();
                originX = lastX;
                originY = lastY;
                break;
            case MotionEvent.ACTION_MOVE:
                int dx = (int) event.getRawX() - lastX;
                int dy = (int) event.getRawY() - lastY;
                int l = v.getLeft() + dx;
                int b = v.getBottom() + dy;
                int r = v.getRight() + dx;
                int t = v.getTop() + dy;
                // 下面判断移动是否超出屏幕
                if (l < 0) {
                    Log.i("超出屏幕","l:"+l);
                    l = 0;
                    r = l + v.getWidth();
                }
                if (t < 0) {
                    Log.i("超出屏幕","t:"+t);
                    t = 0;
                    b = t + v.getHeight();
                }
                if (r > screenWidth) {
                    Log.i("超出屏幕","r:"+r);
                    r = screenWidth;
                    l = r - v.getWidth();
                }
                Log.i("zxcs","b:"+b+"~~~"+(screenHeight-v.getHeight()));
                if (b > screenHeight-v.getHeight()) {
                    Log.i("zxcs","b:"+b+"v.height"+v.getHeight());
                    //b = screenHeight;
                    b=screenHeight-v.getHeight();
                    
                    t = b - v.getHeight();
                    Log.i("zxcs","t:"+t);
                    //b = b - v.getHeight();

                }
                Log.i("zxc","l:"+l+"~~t:"+t+"~~r:"+r+"~~b:"+b);
                v.layout(l, t, r, b);

                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                v.postInvalidate();
                break;
            case MotionEvent.ACTION_UP:
                int distance = (int) event.getRawX() - originX + (int)event.getRawY() - originY;
                Log.e("DIstance",distance+"");
                if (Math.abs(distance)<20) {
                    //当变化太小的时候什么都不做 OnClick执行
                }else {
                    return true;
                }
                break;
        }
        return false;

    }
}
